package com.cpa.admin.config;

import com.cpa.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author zhen.zhao01
 * @CreateDate 2018/11/14
 */
public class BaseController {

    @Autowired
    private UserService userService;

    @ModelAttribute
    public void beforeController(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {

        String loginUser = (String) session.getAttribute(WebSecurityConfig.SESSION_KEY);
        if (!userService.isAdmin(loginUser)) {
            response.sendRedirect("user_page");

        }
    }


}
